<template>
  <div class="withDraw">
    <header class="header">
      <router-link to="/" class="back-link">
        <img src="@/assets/images/return.png" alt="" />
      </router-link>
      <h1 class="page-title">提现</h1>
    </header>
    <section>
      <div class="card">
        <div class="card-header">
          <div class="info_box1">
            <div class="balance-label">USDT余额</div>
            <div
              class="balance-value usdt"
              :style="getValueStyle(infoStore.infomation.usdt)"
            >
              ${{ parseFloat(infoStore.infomation.usdt).toFixed(2) }}
            </div>
          </div>
          <div class="info_box2">
            <div class="balance-label text_color">Da You余额</div>
            <div
              class="balance-value da-you"
              :style="getValueStyle(infoStore.infomation.dayou)"
            >
              {{ parseFloat(infoStore.infomation.dayou).toFixed(2) }}
            </div>
          </div>
        </div>
      </div>

      <div class="body_content">
        <div class="withdraw-type mt-8">
          <div class="withdraw-label">提现类型</div>
          <div class="withdraw-buttons">
            <van-field name="radio" class="field_box">
              <template #input>
                <van-radio-group
                  v-model="checked"
                  direction="horizontal"
                  shape="dot"
                >
                  <van-radio
                    v-for="item in radioList"
                    :key="item.id"
                    :name="item.name"
                    checked-color="#FCBC35"
                    >{{ item.name }}</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
          </div>
        </div>

        <div class="withdraw-amount">
          <div class="withdraw-label">提现数量</div>
          <div class="input_box">
            <van-field
              v-model="amount"
              type="number"
              class="input-field"
              placeholder="请输入提现数量"
              autocomplete="off"
            />
          </div>
        </div>

        <div class="fee-info">
          手续费:
          <span>{{
            checked === "USDT"
              ? infoStore.config.withdrawal_fee_usdt_rate || "0"
              : infoStore.config.withdrawal_fee_rate || "0"
          }}</span>
        </div>

        <van-button class="btn-primary mt-8" @click="handleWithDraw"
          >确定</van-button
        >
      </div>
    </section>
  </div>
  <MyDialog
    v-model:show="dialogVisible"
    title="提示"
    :content="`是否继续提现 ${amount} ${checked} ？`"
    @confirm="onConfirm"
  />
</template>

<script setup lang="ts">
import { showLoadingToast, showConfirmDialog, showToast } from "vant";
import type { RadioList } from "@/types";
// 提现
import { Apitoken, Withdrawal } from "@/api/modules/RechangeW";
import { useInfoStore } from "@/store/infoman";
const infoStore = useInfoStore();
const amount = ref<string>("");
// 选中的值
const checked = ref<string>("USDT");
// 单选框列表
const radioList = ref<RadioList[]>([
  {
    id: 0,
    name: "USDT",
    type: "usdt",
  },
  {
    id: 1,
    name: "DAYOU",
    type: "dayou",
  },
]);

const handleWithDraw = () => {
  if (amount.value !== "") {
    getApitoken();
    dialogVisible.value = true;
  }
};
// 获取apitoken
const dialogVisible = ref<boolean>(false);
const apiToken = ref<string>("");
const getApitoken = async () => {
  try {
    const res = await Apitoken();
    if (res.code === 0) {
      apiToken.value = res.data.api_token;
    }
  } catch (err) {
    console.log(err);
  }
};
const onConfirm = async () => {
  getWithdrawal();
  dialogVisible.value = false;
};
// 提现
const getWithdrawal = async () => {
  showLoadingToast({
    message: "加载中...",
    forbidClick: true,
  });
  try {
    const data = {
      amount: amount.value,
      currency: checked.value,
    };
    const headers = {
      APITOKEN: apiToken.value,
    };
    const res = await Withdrawal(data, headers);
    amount.value = "";
    if (res.code === 0) {
      showToast(res.msg);
      infoStore.getInfo();
    } else {
      showToast(res.msg);
    }
  } catch (err) {
    console.log(err);
  }
};

const getValueStyle = (value: string | number) => {
  const numberValue = Number(value);
  if (numberValue > 100000) {
    return {
      fontSize: "14px", // Smaller font size for large numbers
    };
  }
  return {
    fontSize: "25px", // Default font size for smaller numbers
  };
};
onMounted(() => {
  infoStore.getInfo();
});
</script>

<style scoped lang="less">
@primary-color: #3b82f6; // Tailwind's primary color (blue-500)
@secondary-color: #ef4444; // Tailwind's secondary color (red-500)
@background-color: #f8fffc;
@gray-light: #22825c; // Tailwind's gray-300

.withDraw {
  height: 100vh;
  /* Header Section */
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: linear-gradient(91deg, #f8f8ed 0%, #f5f9f1 50%, #d4f5ea 100%);
    padding: 16px 9px 16px 0px;

    .back-link {
      margin-left: 23px;

      img {
        width: 21px;
        height: 21px;
      }
    }

    .page-title {
      position: absolute;
      top: 15px;
      left: 50%;
      transform: translate(-50%, -50%); /* Centered title */
      font-size: 25px;
      font-weight: bold;
      color: #165f4c;
    }
  }
  section {
    padding: 21px;
    padding-top: 34px;
    /* Card Section */
    .card {
      padding: 11px;
      border-radius: 10px;
      border: 1px solid #ffffff;
    }

    .card-header {
      display: flex;
      background: #edf9f4;
      border-radius: 10px;
      padding: 25px;

      .info_box1,
      .info_box2 {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
      }

      .info_box1 {
        border-right: 1px solid #2f8463;
        padding-right: 19px;
      }
      .info_box2 {
        padding-left: 19px;
      }
    }

    .balance-label {
      font-size: 12px;
      color: #22825c;
      margin-bottom: 8px;
    }

    .text_color {
      color: #fcbc35;
    }

    .balance-value {
      font-size: 25px;
      font-weight: bold;

      &.usdt {
        color: #22825c;
      }

      &.da-you {
        color: #fcbc35;
      }
    }

    /* Body Content Section */
    .body_content {
      margin-top: 23px;
      padding: 20px;
      background: linear-gradient(91deg, #f1fef7 0%, #f9f5e6 100%);
      border-radius: 10px;
    }

    .withdraw-label {
      font-size: 16px;
      color: @gray-light;
      margin-bottom: 8px;
    }

    .withdraw-buttons {
      display: flex;
      gap: 16px;
      background: #dbf7ee;
      border-radius: 10px;
      padding: 8px;

      .field_box {
        background-color: transparent;
      }
    }

    .withdraw-btn {
      flex: 1;
      padding: 8px;
      border-radius: 8px;
      background-color: transparent;
      text-align: center;
      font-size: 16px;

      &.selected {
        background-color: white;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      }
    }

    .withdraw-amount {
      width: 100%;
      margin-top: 15px;

      .input_box {
        padding: 8px 0px 8px 18px;
        font-size: 19px;
        background: #dbf7ee;
        border-radius: 10px;

        .input-field {
          background-color: transparent;
        }
      }
    }

    .fee-info {
      font-size: 12px;
      color: @gray-light;
      margin-top: 13px;
      margin-bottom: 29px;
      text-align: right;
    }

    /* Button Section */
    .btn-primary {
      background: linear-gradient(158deg, #31dfb0 0%, #0fc68a 100%);
      border-radius: 52px;
      color: white;
      padding: 10px;
      border: none;
      text-align: center;
      width: 100%;
      font-size: 19px;
      box-shadow: 0px 0px 1px 3px rgba(2, 232, 155, 0.5);
    }
  }
}
</style>
